<?php
/**
 * NAME: logger
 * WHEN: OnMsg OnJoin OnLeave
 * PROVIDES: !log
 * AUTOLOAD: false
 * DESCRIPTION: Logowanie wiadomości
 * VERSION: 0.1
 * UPDATE: http://update.tempaq2.blacksoft.eu/check.php
 * AUTHOR: blacksoft.eu
 */
  
 exit();
//*module_register_start
	$cmd = $module->addCommand('!log', '_lang_log_desc');
	//admin commands
	$module->addSubcommand($cmd, 'size', '_lang_size_desc', null, true);
	$module->addSubcommand($cmd, 'count', '_lang_count_desc', null, true); #2
	$module->addSubcommand($cmd, 'clearold', '_lang_clearold_desc', '[0-9]{0,2}', true);
	$module->addSubcommand($cmd, 'last_save', '_lang_log_save_desc', null, true);
	//przelaczniki
	$module->addSwitch($cmd, '-n', '_lang_lines_desc', '[0-9]{1,2}'); #5
	$module->addSwitch($cmd, '-t', '_lang_time_desc', '[0-9]{1,4}');
	$module->addSwitch($cmd, '-u', '_lang_user_desc', '[^ ]{0,20}'); #7
	$module->addSwitch($cmd, 'c', '_lang_channel_desc', '#.{0,25}');
	$module->addSwitch($cmd, '-i', '_lang_ident_help', '~.{0,20}'); #9
	$module->addSwitch($cmd, '-o', '_lang_order_desc', '[01]{1}');
	$bot->memo('last_save', 0);
	
//*function_start
	//zajmiemy sie logowaniem wiadomości:) (przy okazji testując prędkośc table_files :P)
	if ($data->type == SMARTIRC_TYPE_QUERY || $data->type == SMARTIRC_TYPE_CHANNEL) {
		if (count($parsed_module)) {						
			//czyste tablice:
			$cols = array();
			$vals = array();
			$ops = array();
			//skanujemy calosc
			foreach ($parsed_module as $k=>$v) {
				$single = $v[0];
				switch ($k) {
					case 1:
						//$table = new table (BOTNAME.'.log');
						$all = sqlite::fetchSQL('SELECT * FROM log ORDER BY timestamp DESC');
							//$all = $table->get();
							foreach ($all as &$b) $b = implode(' ', $b);
							
							$all = implode("\n", $all);
							$size = strlen($all);
						unset($all);
						//$size = filesize(CONF_TABLE_PREFIX.BOTNAME.'.log');
						$bot->send_text('log_size', round($size/1024, 1).'KB');
						break;
					case 2:
						//$table = new table (BOTNAME.'.log');
						//$count = count($table->get());
						$count = sqlite::fetchSQL('SELECT COUNT(*) AS ilosc FROM log');
						$count = $count[0]['ilosc'];
							
						//unset($table);
						$bot->send_text('log_count', $count);
						break;
					case 3:
						if (!is_numeric($v[0])) $v[0]=14;
						
						//$table = new table (BOTNAME.'.log');
						$count1 = sqlite::fetchSQL('SELECT COUNT(*) AS ilosc FROM log');
						$count1 = $count[0]['ilosc'];
						
						sqlite::query('DELETE FROM log WHERE timestamp<="'.time()-($v[0]*24*3600).'"');
						
						$count2 = sqlite::fetchSQL('SELECT COUNT(*) AS ilosc FROM log');
						$count2 = $count[0]['ilosc'];
						
						$ile = $count2 - $count1;

						$bot->send_text('log_clear', $ile);
						break;
					case 4:
						if ($time = $bot->memo('last_save') !== 0) {
							$bot->send_text('save_time', $time);
						}
						break;
					case 5:
						if ($single == 0)
							$parsed_module[$k][0] = 10;
						break;
					case 6:
						$cols[] = 'timestamp';
						$vals[] = time()-($single*60);
						$ops[] = '>=';
						break;
					case 7:
						$cols[] = 'nick';
						$vals[] = $single;
						$ops[] = '=';
						break;
					case 8:
						$cols[] = 'channel';
						$vals[] = $single;
						$ops[] = '=';
						break;
					case 9:
						$cols[] = 'ident';
						$vals[] = $single;
						$ops[] = '=';
						break;
						
				}
			}
			if (count($cols) || isset($parsed_module[5])) {
				//$table = new table(BOTNAME.'.log');
				
				$bot->sendmessage('Temporary unavailable');
				
				//$dane = sqlite::fetchSQL('SELECT * FROM log WHERE ')
				/*
					$dane = $table->select($cols, $vals, $ops, ((isset($parsed_module[5][0]))?$parsed_module[5][0]:5));
					if (is_array($dane)) $table->sort($dane, 'timestamp', (isset($parsed_module[10][0])?$parsed_module[10][0]:0));
				unset($table);
				if (count($dane) && is_array($dane)) {
					foreach ($dane as $v) {
						$array[]='['.date('H:i.s d M', $v['timestamp']).'] ('.$v['channel'].') '.$v['nick'].' '.$v['msg'];	
					}
					$bot->sendarray($array);
				}
				*/
			}
		}
	}
	if (!count($parsed_module)) {
		$msg = null;
		switch ($data->type) {
			case SMARTIRC_TYPE_CHANNEL:
				$msg = $data->message;
				break;
			case SMARTIRC_TYPE_PART:
				$msg = 'leaves channel '.$data->channel;
				break;
			case SMARTIRC_TYPE_JOIN:
				$msg = 'joins channel '.$data->channel;
				break;
		}
		if (!empty($msg)) {
			$start = microtime(true);

			sqlite::insert('log',array(
				'channel' => $data->channel,
				 'timestamp' => time(),
				 'nick' => $data->nick,
				 'ident' => $data->ident,
				 'msg' => $msg
			));

			
			$time = microtime(true)-$start;
			$bot->memo('last_save', $time);
			//	$bot->debug('Saving time: '.$time, DBG_INFO, 2);
		}
	}
//*function_end
?>